#include "Shell.h"
#include <stdio.h>
#include <iostream>
//#include <fcntl.h>
#include <fstream>
//#include <string>

Shell1::Shell1(int size):Thread(DisplayShell,(void*)myData)
{
//fcntl(0,F_SETFL,O_NONBLOCK);
mailbox=new BoundedMailBox(size);
}

Shell1::~Shell1()
{
delete msg;
delete mailbox;
}

void Shell1::setMailBoxSize(int size)
{
mailbox->setSize(size);
}

int Shell1::getMailBoxSize()
{
	return mailbox->getSize();
}

int Shell1::getMailBoxMaxSize()
{
	return mailbox->getMaxSize();
}

void Shell1::cleanMailbox()
{
	mailbox->cleanMailBox();
}

void* Shell1::DisplayShell(void* arg)
{
arg=new char[80];
Message* msg2;
Message* n;
char** command=new char*[80];
command[0]=NULL;
bool exit=false;
bool created=false;
SystemCalls* system=SystemCalls::getInstance();
while(!exit)
{
	arg=new char[80];
	printf("The-Mighty-os-prompt>>");
	cout.flush();
	//fgets((char*)arg,80,stdin);
	//int count=read(0,(char*)arg,80);
	arg=readString();
	msg2=new Message((char*)arg);
	command=msg2->getBrokenMessage();
	/*************
	 *user commands
	 **************/
	 /*if(count<1)
	 {
	 	if(created)
		{
			
			while (system->numofmessages(-1) != 0) {
			n=system->Receive(-1);
			if (n->type() == 1) {
				cout << "\n process " << n->dest() << " recieved text message: " << n->text() << "\n";
			}
			if (n->type() == 2) {
				cout << "\n process " << n->from() << " passed text message " << n->text() << " to "<<  n->dest() << "\n";
			}
			if (n->type() == 3) {
				cout << "\n process "<< n->from() << " discarded text message "<< n->text() << "\n";
			}
			if (n->type() == 4) {
				cout<< n->dest() << "'s table is:\n";
  				n->getRT()->printRT();  
			}
			printf("\nThe-Mighty-os-prompt>>");
			cout.flush();	
			}
		}
	}*/
	 if(command[0]==NULL)//we've added this due to the fact that readString is IO BLOCKING So if the 
		 //user wants the shell to extract a message recieved by a node (passing through 
		 //the mailer) he needs to press enter or a command. the notes above can 
		 //transform it to non io blocking but it is busy waiting style very bad 
		 //for the cpu
	{
		if(created)
		{
			
			while (system->numofmessages(-1) != 0) {
			n=system->Receive(-1);
			
			if (n->type() == 1) {
				cout << "\n process " << n->dest() << " recieved text message: " << n->text() << "\n";
			}
			if (n->type() == 2) {
				cout << "\n process " << n->from() << " passed text message " << n->text() << " to "<<  n->dest() << "\n";
			}
			if (n->type() == 3) {
				cout << "\n process "<< n->from() << " discarded text message "<< n->text() << "\n";
			}
			if (n->type() == 4) {
				cout<< n->dest() << "'s table is:\n";
  				n->getRT()->printRT();  
			}
			}
		}
		else
			continue;
	}
	//1.this case is CreateNet
	else if (!strcmp(command[0],"CreateNet"))
	{
		//printf("%d\n",system->CreateNet(command[1]));
		if(system->CreateNet(command[1])!=-1)
			created=true;
	}
	//2.this case is Time
	else if (!(strcmp(command[0],"Time"))&& (command[1]==NULL||command[1]==" "))
		printf("the total CPU time given to all therads since creation is: %lf\n", system->Time());
	//3.this case is Time i
	else if (!(strcmp(command[0],"Time")) && !(command[1]==NULL||command[1]==" "))
	{
		if (system->Time(atoi(command[1]))==-3)
			printf("Error this thread was not created yet in the system\n");
		else if (!(strcmp(command[1],"-2")))
			printf("the total CPU time the Shell got since last CreateNet was: %lf \n",system->Time(atoi(command[1])));
		else if (!(strcmp(command[1],"-1")))
			printf("the total CPU time the Mailer got since last CreateNet was: %lf \n",system->Time(atoi(command[1])));
		else	
		printf("the total CPU time thread %s got since creation was: %lf \n",command[1],system->Time(atoi(command[1])));
	}
	//4.this case is Max_Length i
	else if(!(strcmp(command[0],"Max_Length"))&& (command[1]==NULL||command[1]==" "))
		printf("the Mailers max mailbox size since last CreateNet was: %d \n",system->Max_Length());
	//5.this case is Max_Length i
	else if(!(strcmp(command[0],"Max_Length"))&& !(command[1]==NULL||command[1]==" "))
	{
		if (system->Max_Length(atoi(command[1]))==-3)
			printf("Error this thread was not created yet in the system\n");
		else if (!(strcmp(command[1],"-2")))
			printf("the Shells max mailbox size since last CreateNet was: %d  \n",system->Max_Length(-2));
		else	
		printf("Thread #%s max mailbox size since creation is: %d \n",command[1],system->Max_Length(atoi(command[1])));
	}
	//6.this case is SendText
	else if(!strcmp(command[0],"SendText"))
	{
		system->SendText(atoi(command[1]),atoi(command[2]),command[3]);
	}
	//7.this case is CrashNode i
	else if(!strcmp(command[0],"CrashNode"))
	{
		system->CrashNode(atoi(command[1]));
	}
	//8.this case is ReviveNode i
	else if(!strcmp(command[0],"ReviveNode"))
	{
		system->ReviveNode(atoi(command[1]));
	}
	//9.this case is ShowTable i
	else if(!strcmp(command[0],"ShowTable"))
	{
		system->ShowTable(atoi(command[1]));
	}
	//10.this case is KillAll
	else if(!strcmp(command[0],"KillAll"))
	{
		system->KillAll();
		created=false;
	}
	//11.this is exit this does not work due to buggy Thread package
	else if(!strcmp(command[0],"exit"))
	{
		exit=true;
	}
	//Unknown Command
	else
	{
		printf("prompt: %s: command not found\n",command[0]);
		//continue; 
	}
	if(created)
		{
			
			while (system->numofmessages(-1) != 0) {
			n=system->Receive(-1);
			if (n->type() == 1) {
				cout << "\n process " << n->dest() << " recieved text message: " << n->text() << "\n";
			}
			if (n->type() == 2) {
				cout << "\n process " << n->from() << " passed text message " << n->text() << " to "<<  n->dest() << "\n";
			}
			if (n->type() == 3) {
				cout << "\n process "<< n->from() << " discarded text message "<< n->text() << "\n";
			}
			if (n->type() == 4) {
				cout<< n->dest() << "'s table is:\n";
  				n->getRT()->printRT();  
			}
			}
		}
	
//	self()->block();
//	return (NULL);
}
delete(msg2);
self()->exit();
return(NULL);
}

int Shell1::AmIBlocked()
{
	return isBlocked();
}

char** Shell1::getCommand()
{
	msg=new Message(myData);
	return msg->getBrokenMessage();
}

BoundedMailBox* Shell1::getMailBox()
{
	return mailbox;
}
